package kr.co.cherryt.activity;

import kr.co.cherryt.cherryt.R;
import kr.co.cherryt.cherryt.UpdateChecker;
import kr.co.cherryt.cherryt.UpdateChecker.CheckResultLisnter;
import kr.co.cherryt.common.CC_Log;
import kr.co.cherryt.common.CherrytData;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;

public class IntroActivity extends Activity {
	
	private UpdateChecker m_updateChecker;

	/*Handler handler = new Handler();

	Runnable fisishRunnable = new Runnable() {
		public void run() {
			finish();
		}
	};
*/
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		new Handler() {
			public void handleMessage(Message msg) {
				splashCore();
			}			
		}.sendEmptyMessageDelayed(0, 1500);
		
		setContentView(R.layout.intro_activity);
		
		//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		//handler.postDelayed(fisishRunnable, 2000);	// 종료 작업 예약
		//Log.d("lifecycle", "intro 1. onCreate" );
	}

	/*@Override
	protected void onDestroy() {
		handler.removeCallbacks(fisishRunnable);	// 대기열의 작업 취소 (작업 실행 전)
		super.onDestroy();
	}*/
	
	private void splashCore() {
		// 업데이트를 체크한다
		startUpdateCheck();
	}
	
	/**
	 * app의 update 체크를 시작한다
	 */
	private void startUpdateCheck(){
		
		if(m_updateChecker == null){
			m_updateChecker = new UpdateChecker(IntroActivity.this);
		}
			
		String szUpdateCheckURL = CherrytData.getXmlUrl(IntroActivity.this, "ct_version_check_android.php");
		CC_Log.info("jin", "### szUpdateCheckURL : " + szUpdateCheckURL);
        m_updateChecker.setCheckResultLisnter(m_updateResult);
        m_updateChecker.start(szUpdateCheckURL);
	}
	
	// 업데이트 체크 결과에 대한 리스너 
    CheckResultLisnter m_updateResult = new CheckResultLisnter() {
 
        @Override
        public void onCheckError(int nErrcode, UpdateChecker obj) {
            // 에러 발생시
            new AlertDialog.Builder(IntroActivity.this)
            .setTitle("확인")
            .setMessage(CherrytData.ERR_NETWORK_MSG + " " + nErrcode)
            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    IntroActivity.this.finish();
                }
            }).create().show();
        }
 
        @Override
        public void onMarketLanding(int nAppVer, int nServerVer,UpdateChecker obj) {
            // 마켓으로 랜딩 한 후에 호출되는 함수 (보편적으로는 앱을 종료시킨다) 
        	IntroActivity.this.finish();            
        }
 
        @Override
        public void onUpdatePass(int nAppVer, int nServerVer) {
        	CC_Log.info("jin", "### onUpdatePass");
            // 업데이트 체크를 통과했으므로  다음에 보여질 Activity를 생성한다.  
            startNextActivity();
        }
    };
    
    private void startNextActivity() {
    	Intent startIntent = new Intent(IntroActivity.this, CherryTActivity.class);
    	startActivity(startIntent);
    	IntroActivity.this.finish();
    }
}
